import java.util.Arrays;
import java.util.Random;
import java.util.Stack;

/**
 * @program: sort
 * @author: JCM
 * @create: 2023-03-13 13:59
 * @description:
 **/
public class Main {
    public static void main(String[] args) {
        int[] arr = {4,3,5,2,6,1,7,9,8,0};

        //insertSort(arr);
        //shellSort(arr);
        //selectSort(arr);
        //selectSort2(arr);
//        heapSort(arr);
        //bubbleSort(arr);
        //quickSort(arr);
        mergeSort(arr);
        System.out.println(Arrays.toString(arr));


    }

    /**
     * 插入排序
     * @param arr
     */
    private static void insertSort(int[] arr) {
        for (int i = 1; i < arr.length; i++) {
            int tmp = arr[i];
            int j = i;
            for (; j > 0; j--) {
                if (tmp < arr[j-1]) {
                    arr[j] = arr[j-1];
                }else {
                    break;
                }
            }
            arr[j] = tmp;
        }
    }

    /**
     * 希尔排序
     * @param arr
     */
    private static void shellSort(int[] arr) {
        int gap = arr.length;
        gap /= 2;
        while (gap > 1) {
            shell(arr,gap);
            gap /= 2;
        }
        shell(arr,1);
    }

    private static void shell(int[] arr, int gap) {
        for(int i = gap; i < arr.length; i++) {
            int tmp = arr[i];
            int j = i-gap;
            for(; j >= 0; j -= gap) {
                if (tmp < arr[j]) {
                    arr[j+gap] = arr[j];
                }else {
                    break;
                }
            }
            arr[j+gap] = tmp;
        }
    }

    /**
     * 直接选择排序
     * @param arr
     */
    private static void selectSort(int[] arr) {
        for(int i = 0; i < arr.length; i++) {
            int minIndex = i;
            for (int j = i; j < arr.length; j++) {
                minIndex = arr[minIndex] > arr[j] ? j : minIndex;
            }
            swap(arr,i,minIndex);
        }
    }

    /**
     * 双指针选择排序
     * @param arr
     */
    private static void selectSort2(int[] arr) {
        int left = 0;
        int right = arr.length-1;
        while (left < right) {
            int minIndex = left;
            int maxIndex = left;
            for(int j = left+1; j <= right; j++) {
                minIndex = arr[minIndex] > arr[j] ? j : minIndex;
                maxIndex = arr[maxIndex] < arr[j] ? j : maxIndex;
            }
            swap(arr,left,minIndex);
            // 修正 max 下标
            if (left == maxIndex) {
                maxIndex = minIndex;
            }
            swap(arr,right,maxIndex);
            left++;
            right--;
        }
    }

    /**
     * 堆排序
     * @param arr
     */
    private static void heapSort(int[] arr) {
        // 建大根堆
        createBigHeap(arr);
        System.out.println("建堆后："+Arrays.toString(arr));
        int end = arr.length-1;
        while (end > 0) {
            swap(arr,0,end);
            shiftDown(arr,0,end);
            end--;
        }
    }

    private static void createBigHeap(int[] arr) {
        for (int parent = (arr.length-1-1)/2; parent >= 0; parent--) {
            //向下调整
            shiftDown(arr,parent,arr.length);
        }
    }

    private static void shiftDown(int[] arr, int parent, int length) {
        // 判断左孩子
        int child = parent*2+1;
        while(child < length) {
            if (child + 1 < length && arr[child+1] > arr[child]) {
                child++;
            }
            if(arr[child] > arr[parent]) {
                swap(arr,child,parent);
                parent = child;
                child = parent*2+1;
            }else {
                break;
            }
        }
    }


    private static void bubbleSort(int[] arr) {
        for (int i = 0; i < arr.length-1; i++) {
            boolean flg = false;
            for (int j = 0; j < arr.length-i-1; j++) {
                if (arr[j] > arr[j+1]) {
                    swap(arr,j,j+1);
                }else {
                    flg = !flg;
                }
            }
            if (flg) {
                break;
            }
        }
    }

    /**
     * 优化后的快排
     * @param arr
     */
    private static void quickSort3(int[] arr) {

        quickHole(arr,0,arr.length-1);
    }

    /**
     * 三数取中，解决前面数字有序的情况
     * @param arr
     * @param left
     * @param right
     * @return
     */
    private static int midNumIndex(int[] arr, int left, int right) {
        int mid = (left+right)/2;
        if(arr[left] < arr[right]) {
            if(arr[mid] < arr[left]) {
                return left;
            }else if (arr[mid] > arr[right]) {
                return right;
            }else {
                return mid;
            }
        }else {
            if (arr[mid] < arr[right]) {
                return right;
            }else if(arr[mid] > arr[left]) {
                return left;
            }else {
                return mid;
            }
        }
    }

    /**
     * 快排调用的修改后的插入排序
     * @param arr
     */
    private static void insertSort2(int[] arr,int start, int end) {
        for (int i = start+1; i <= end; i++) {
            int tmp = arr[i];
            int j = i;
            for (; j >= start; j--) {
                if (tmp < arr[j-1]) {
                    arr[j] = arr[j-1];
                }else {
                    break;
                }
            }
            arr[j] = tmp;
        }
    }
    private static void quickSort2(int[] arr) {
        quickHole(arr,0,arr.length-1);
    }

    private static void quickHole(int[] arr, int left, int right) {
        if (left >= right) {
            return;
        }
        // 小区间使用直接插入排序，减少递归深度
        if (right - left + 1 <= 7) {
            // 使用直接插入排序
            insertSort2(arr,left,right);
            return;
        }
        // 解决递归深度问题
        int index = midNumIndex(arr,left,right);
        swap(arr,left,index);
        int pivot = partitionHole(arr,left,right);
        quickHole(arr,left,pivot-1);
        quickHole(arr,pivot+1,right);
    }
    private static int partitionHole(int[] arr, int left, int right) {
        int key = arr[left];
        while (left < right) {
            while (left < right && arr[right] >= key) {
                right--;
            }
            arr[left] = arr[right];
            while (left < right && arr[left] <= key) {
                left++;
            }
            arr[right] = arr[left];
        }
        arr[left] = key;
        return left;
    }

    private static void quickSort1(int[] arr) {
        quick(arr,0,arr.length-1);
    }

    private static void quick(int[] arr, int left, int right) {
        if (left >= right) {
            return;
        }
        int pivot = partitionHoare(arr,left,right);
        quick(arr,left,pivot-1);
        quick(arr,pivot+1,right);
    }

    /**
     * N*logN ---理想情况每次都均分数据；最坏情况O(N^2)数组有序的时候
     * @param arr
     * @param left
     * @param right
     * @return
     */
    private static int partitionHoare(int[] arr, int left, int right) {
        // 事先存储好 left 和 right 下标
        int i = left;
        int j = right;
        int pivot = arr[left];
        while (i < j) {
            // 一定要先走右边
            while (i < j && arr[j] >= pivot) {
                j--;
            }
            while (i < j && arr[i] <= pivot) {
                i++;
            }
            swap(arr,i,j);
        }
        swap(arr,i,left);
        return i;
    }

    /**
     * 非递归实现快排
     * @param arr
     */
    private static void quickSort(int[] arr) {
        Stack<Integer> stack = new Stack<>();
        int left = 0;
        int right = arr.length-1;
        int pivot = partitionHole(arr,left,right);
        if (pivot > left+1) {
            stack.push(left);
            stack.push(pivot-1);
        }
        if (pivot < right-1) {
            stack.push(pivot+1);
            stack.push(right);
        }
        while (!stack.isEmpty()) {
            right = stack.pop();
            left = stack.pop();
            pivot = partitionHole(arr,left,right);
            if (pivot > left+1) {
                stack.push(left);
                stack.push(pivot-1);
            }
            if (pivot < right-1) {
                stack.push(pivot+1);
                stack.push(right);
            }
        }
    }

     private static void mergeSort1(int[] arr) {
        mergeSortFunc(arr,0,arr.length-1);
    }

    private static void mergeSortFunc(int[] arr, int left, int right) {
        // 结束条件
        if (left >= right) {
            return;
        }
        int mid = (left+right)/2;
        mergeSortFunc(arr,left,mid);
        mergeSortFunc(arr,mid+1,right);
        // 合并
        merge(arr,left,right,mid);
    }
    private static void merge(int[] arr, int start, int end, int midIndex) {
        int s1 = start;// 第一个归并段的起点
        int s2 = midIndex+1;// 第二个归并段的起点
        int[] tmp = new int[end-start+1];
        int k = 0;
        while (s1 <= midIndex && s2 <= end) {
            if (arr[s1] <= arr[s2]) {
                tmp[k++] = arr[s1++];
            }else {
                tmp[k++] = arr[s2++];
            }
        }
        // 当一个归并段中没有数据之后
        while (s1 <= midIndex) {
            tmp[k++] = arr[s1++];
        }
        while (s2 <= end) {
            tmp[k++] = arr[s2++];
        }
        // 将 tmp 中的数据拷贝回原数组
        for (int i = 0; i < k; i++) {
            arr[i+start] = tmp[i];
        }
    }

    /**
     * 非递归实现归并排序
     * @param arr
     */
    private static void mergeSort(int[] arr) {
        int gap = 1;
        while (gap < arr.length) {
            for (int i = 0; i < arr.length; i += gap*2) {
                int s1 = i;
                int e1 = s1+gap-1;
                if (e1 >= arr.length) {
                    e1 = arr.length-1;
                }
                int s2 = e1+1;
                if (s2 >= arr.length) {
                    s2 = arr.length-1;
                }
                int e2 = s2+gap-1;
                if (e2 >= arr.length) {
                    e2 = arr.length-1;
                }
                merge(arr,s1,e2,e1);
            }
            gap *= 2;
        }

    }

    /**
     * 时间复杂度：O(N+范围)
     * 空间复杂度：O(范围)
     * 稳定性：
     * 计数排序：适合 给定一个范围的数据 进行排序
     * @param array
     */
    public static void countSort(int[] array) {
        int maxVal = array[0];
        int minVal = array[0];

        for (int i = 0; i < array.length; i++) {
            if(array[i] < minVal) {
                minVal = array[i];
            }
            if(array[i] > maxVal) {
                maxVal = array[i];
            }
        }
        //就能确定当前数组的最大值 和 最小值
        int len = maxVal - minVal + 1;
        int[] count = new int[len];
        //开始遍历array数组 进行计数
        for (int i = 0; i < array.length; i++) {
            int val = array[i];
            count[val-minVal]++;

        }

        int index = 0;//array数组的下标
        for (int i = 0; i < count.length; i++) {
            //确保 当前count数组 可以检查完
            while (count[i] != 0) {
                array[index] = i+minVal;
                index++;
                count[i]--;
            }
        }
    }

    private static void swap(int[] arr, int i, int j) {
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }

}
